package work.soho.proxy.ui;

import work.soho.proxy.ProxyApplication;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

import static work.soho.proxy.ProxyApplication.infoBox;

/**
 * 托盘程序UI
 */
public class TrayIconUi extends TrayIcon {

    public TrayIconUi(Image image) {
        super(image);
        setImage(image);
        setToolTip("阿里桌面助手");
        PopupMenu menu = new PopupMenu();

        //重启
        MenuItem restartItem = new MenuItem("Restart proxy");
        restartItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    ProxyApplication.proxyDaemon.check();
                    infoBox("重启成功！", "阿里代理");
                } catch (Exception checkE) {
                    checkE.printStackTrace();
                }
            }
        });
        menu.add(restartItem);
        //释放代理服务器
        MenuItem destroyServerItem = new MenuItem("Destroy server");
        destroyServerItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    ProxyApplication.proxyDaemon.dropServer();
                    infoBox("代理服务器删除成功", "阿里代理");
                } catch (Exception deleteException) {
                    deleteException.printStackTrace();
                    //Ignore
                }

            }
        });
        menu.add(destroyServerItem);

        //配置阿里云账号信息
        MenuItem configAliyunKeyItem = new MenuItem("Configure");
        configAliyunKeyItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.printf(System.getProperty("user.home"));
                ConfigureFrame.instance().show();
            }
        });
        menu.add(configAliyunKeyItem);

        //换个服务器
        MenuItem switchServeItem = new MenuItem("Switch server");
        switchServeItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    ProxyApplication.proxyDaemon.switchService();
                    infoBox("重启成功！", "阿里代理");
                } catch (Exception deleteException) {
                    deleteException.printStackTrace();
                    //Ignore
                }
            }
        });
        menu.add(switchServeItem);
        //测试代理是否正常
        MenuItem proxyStatusItem = new MenuItem("Proxy status");
        proxyStatusItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if(ProxyApplication.proxyDaemon.isPortUsed()) {
                        infoBox("代理端口监听正常！", "阿里代理");
                    } else {
                        infoBox("代理状态异常！", "阿里代理");
                    }
                } catch (Exception deleteException) {
                    deleteException.printStackTrace();
                    //Ignore
                }
            }
        });
        menu.add(proxyStatusItem);
        //查询lan ip地址
        MenuItem lanDdnsItem = new MenuItem("Look ddns ip");
        lanDdnsItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if(!ProxyApplication.proxyDaemon.getConfigManage().getPartsConfigure().getBaseDomain().equals("")) {
                        infoBox("当前内网域名: lan."+ProxyApplication.proxyDaemon.getConfigManage().getPartsConfigure().getBaseDomain() +
                                        " IP:" + ProxyApplication.proxyDaemon.getDdnsLanIp() + "\r\n"
                                        + "当前公网域名：internet."+ProxyApplication.proxyDaemon.getConfigManage().getPartsConfigure().getBaseDomain() +
                                        " IP:" + ProxyApplication.proxyDaemon.getDdnsWanIp()
                                ,"动态域名IP");
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        menu.add(lanDdnsItem);
        //退出
        MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        menu.add(exitItem);
        setImageAutoSize(true);
        setPopupMenu(menu);
        SystemTray tray = SystemTray.getSystemTray();
        try {
            tray.add(this);
        } catch (AWTException e) {
            e.printStackTrace();
        }

    }
}
